<?php include("../config.php"); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title><?php echo $sitename; ?> - Sysop: Rate Software</title>
<meta name="description" content="<?php echo $sitename; ?> - Sysop: Rate Software">
<meta name="keywords" content="free to try, software, shareware, freeware, developers, vendors, download, PAD">
<meta name="author" content="Andrey Aleksanyants, M.D.">
<meta name="copyright" content="easy HTools">
<meta http-equiv="imagetoolbar" content="no">
<link rel="stylesheet" type="text/css" href="../<?php echo $cssfile; ?>">
<link rel="shortcut icon" href="../favicon.ico">
</head>
<body>
<table class="tmain" align="center" width="90%" cellspacing="0" cellpadding="0">
<tr>
<td class="tmain">
<table class="tlogo" align="center" width="100%" cellspacing="0" cellpadding="4">
<tr>
<td align="right">
<p><a href="index.php">Home</a></p>
</td>
</tr>
</table>
</td>
</tr><tr>
<td class="tmain">
<br>
<table align="center" width="100%" border="0" cellspacing="0" cellpadding="1">
<tr valign="top">
<td class="tbody">
<h1><?php echo $sitename; ?> - Sysop: Rate Software</h1>
<?php

// Requesting form data
$progid  = $_POST["progid"];
$rating  = $_POST["rating"];
$clean   = $_POST["clean"];
$comment = $_POST["comment"];
$submit  = $_POST["submit"];

if(empty($submit)) { // Displaying HTML form

	echo "<form action=\"" . $PHP_SELF . "\" method=\"post\">\n";
	echo "<table class=\"tvisible\" align=\"center\" cellSpacing=\"0\" cellPadding=\"4\">\n";
	echo "<tr>\n";
	echo "<td class=\"tdark\">\n";
	echo "<label for=\"progid\"><p><b>ProgID:</b></p></label></td>\n";
	echo "<td><p><input type=\"text\" class=\"text\" name=\"progid\" maxlength=\"7\"></p></td>\n";
	echo "</tr><tr>\n";
	echo "<td class=\"tdark\">\n";
	echo "<label for=\"rating\"><p><b>Rating:</b></p></label></td>\n";
	echo "<td><p><select name=\"rating\" id=\"rating\" size=\"1\">\n";
	echo "<option value=\"0\" selected>&lt; Select &gt; &nbsp;</option>\n";
	echo "<option value=\"1\">1 - Awful</option>\n";
	echo "<option value=\"2\">2 - Poor</option>\n";
	echo "<option value=\"3\">3 - Good</option>\n";
	echo "<option value=\"4\">4 - Very Good</option>\n";
	echo "<option value=\"5\">5 - Excellent</option>\n";
	echo "</select></p></td>\n";
	echo "</tr><tr>\n";
	echo "<td class=\"tdark\">\n";
	echo "<label for=\"clean\"><p><b>Clean Award:</b></p></label></td>\n";
	echo "<td><p><input type=\"checkbox\" name=\"clean\" value=\"1\"></p></td>\n";
	echo "</tr><tr>\n";
	echo "<td class=\"tdark\">\n";
	echo "<label for=\"comment\"><p><b>Editor comment:</b><br>(Optional)</p></label></td>\n";
	echo "<td><textarea name=\"comment\" cols=\"45\" rows=\"5\"></textarea></td>\n";
	echo "</tr><tr>\n";
	echo "<td class=\"tdark\"><p><b>Submit:</b></p></td>\n";
	echo "<td><input type=\"submit\" class=\"button\" name=\"submit\" value=\"   Submit   \"></td>\n";
	echo "</tr>\n</table></form><br>\n";

}
else { // Processing received data
	if(empty($progid)) { // If the ProgID field is empty
		$bad  = "You didn't specify the ProgID!<br>\n";
	}
	if(empty($clean)) { // If the clean award rating is not set
		$clean  = "0";
	}
	if($bad) { // If there was any problem with filling the form
		echo "<h2 class=\"red\">" . $bad . "</h2>\n";
		echo "<p><b>Please go <a href=\"javascript:history.back()\">Back</a> and fill the form correctly.</b></p>\n";
	}
	else { // Form is OK
		// Connecting to database and inserting the data
		if(!($link_id = mysql_connect($dbhost, $userid, $userpassword))) {
			die("<p><b>Error connecting to the database server:</b> " . mysql_error() . "</p>\n");
		}
		mysql_select_db($dbname);
		$result = mysql_query("UPDATE paddata SET siterating='$rating', clean='$clean' WHERE progid='$progid'", $link_id);
		if($result) { // If data has been added
			// 1. Displaying OK message
			echo "<h2 class=\"green\">The database has been successfully updated:<br></h2>\n";
			echo "<p>ProgID: " . $progid . "</p>\n";
			echo "<p>Rating: " . $rating . "</p>\n";
			echo "<p>Clean: " . $clean . "</p><br>\n";

			// 2. Receiving the required information from paddata
			$result = mysql_query("SELECT * FROM paddata WHERE progid='$progid'", $link_id);
			$query = mysql_fetch_array($result);
			$contactname = $query["contactname"];
			$email       = $query["email"];
			$title       = $query["title"];
			
			// 3. Sending notification email about rating
			$subject  = $title . " has been rated on " . $sitename;
			$message  = "Dear " . $contactname . "!\r\n\r\n";
			$message .= "Your software, \"" . $title . "\", has been reviewed by the staff of " . $sitename . " web site and got " . $rating . " Star award.\r\n\r\n";
			$message .= "You can get the award logo from http://" . $_SERVER['HTTP_HOST'] . "/awards.php and link it to your software description page:\r\nhttp://" . $_SERVER['HTTP_HOST'] . "/details.php?progid=" . $progid . "\r\n\r\n";
			if ($clean) {
				$message .= "In addition, your software received \"100% Clean\" award for absence of any viruses, adware and spyware modules.\r\n\r\n";
			}
			if ($comment) {
				$message .= "The following comment was written by our review stuff while reviewing your software:\r\n";
				$message .= $comment . "\r\n\r\n";
			}
			$message .= "If you have any other software products listed on http://" . $_SERVER['HTTP_HOST'] . " that has not been rated yet, you can submit them for " . $sitename . " award from http://" . $_SERVER['HTTP_HOST'] . "/applyaward.php\r\n\r\n";
			$message .= "If you have any questions or comments please do not hesitate to contact us at " . $sitemail . " or by filling contact form here: http://" . $_SERVER['HTTP_HOST'] . "/contact.php\r\n\r\n";
			$message .= "Kind regards,\r\n\r\n";
			$message .= $sitename . " staff\r\n";
			$headers  = "From: " . $sitename . "<" . $sitemail . ">\r\n";
			$headers .= "Reply-To: " . $sitename . "<" . $sitemail . ">\r\n";
			//$headers.= "Bcc: " . $sitemail . "\r\n"; // to receive the background copies
			$headers .= "Content-Type: text/plain; charset=windows-1252";
			if(mail($email, $subject, $message, $headers)) {
				echo "<p>Email successfully sent to " . $email . "</p>\n";
			}
			else {
				echo "<p>Error sending email to " . $email . "</p>\n";
			}
		}
		else { // If data adding failed
			// 1. Displaying error message
			echo "<h2 class=\"red\">An error occured while updating the database:<br></h2>\n";
			echo "<p>" . mysql_error() . "</p>\n";
			echo "<p><b>Please go <a href=\"javascript:history.back()\">Back</a> to Review Form and try again later.</b></p>\n";
		}
		
		// Disconnecting from database
		mysql_close($link_id);
	}
}
?>
<br>

</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>